package org.moca.activity;

import org.moca.Constants;
import org.moca.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SettingsLogin extends Activity {
	private EditText passcode = null;
	private Button verifyButton;
	private int attemptCounter = 3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.setttings_codescreen);

		passcode = (EditText) findViewById(R.id.settings_codefield);
		verifyButton = (Button) findViewById(R.id.settings_codebutton);
	}

	public void verifyCode(View view) {
		if (passcode.getText().toString()
				.equals(Constants.DEFAULT_SETTINGS_PASSWORD)) {
			attemptCounter = 3;

			Intent go = new Intent(SettingsLogin.this,
					org.moca.activity.settings.Settings.class);
			startActivity(go);
		} else if (passcode.getText().toString().equals("")
				|| passcode.getText().toString().equals("")) {
			Toast.makeText(
					getApplicationContext(),
					"Please input a passocde - " + (attemptCounter)
							+ " attempt/s remaining.", Toast.LENGTH_SHORT)
					.show();
			// decrement attempt counter
			// counter--;

		} else {
			Toast.makeText(
					getApplicationContext(),
					"Incorrect Passcode - " + (attemptCounter - 1)
							+ " attempt/s remaining.", Toast.LENGTH_SHORT)
					.show();

			// decrement attempt counter
			attemptCounter--;

			// disable login on 3 failures
			if (attemptCounter <= 0) {
				verifyButton.setEnabled(false);

				// timer.schedule(onComplete, 0, 2000);
				verifyButton.postDelayed(new Runnable() {
					public void run() {
						verifyButton.setVisibility(View.VISIBLE);
						verifyButton.setEnabled(true);
						attemptCounter = 3;
					}
				}, 7000);
			}
		}
	}
}
